package com.vhall.component.service.room.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vhall.component.dao.room.RoomDraftRecordMapper;
import com.vhall.component.dao.room.RoomProdRelationMapper;
import com.vhall.component.entity.room.entity.RoomDraftRecordEntity;
import com.vhall.component.entity.room.entity.RoomProdRelationEntity;
import com.vhall.component.service.room.RoomProdRelationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author a1111
* @description 针对表【room_prod_relation(房间和产品的关联表,适当性豁免和观看范围限定)】的数据库操作Service实现
* @createDate 2023-10-23 12:03:57
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class RoomProdRelationServiceImpl extends ServiceImpl<RoomProdRelationMapper, RoomProdRelationEntity>
    implements RoomProdRelationService {
    private final RoomDraftRecordMapper roomDraftRecordMapper;
    @Override
    public void relationRoomToGuoxinProd(List<String> exemptProdCodes, Integer roomDraftRecordId, String roomId, Integer ilId, Integer roomAssociationType) {
        if (CollUtil.isNotEmpty(exemptProdCodes)) {
            List<RoomProdRelationEntity> roomProdRelationEntities = getOldRoomProdRelationEntities(roomDraftRecordId, roomId, ilId, roomAssociationType);
            List<RoomProdRelationEntity> insertRoomProdRelation;
//            草稿中有数据的处理方案
            if (CollUtil.isNotEmpty(roomProdRelationEntities)) {
                List<Integer> delIds = roomProdRelationEntities.stream().map(RoomProdRelationEntity::getId).collect(Collectors.toList());
                removeByIds(delIds);
            }
            insertRoomProdRelation = exemptProdCodes.stream().filter(StrUtil::isNotBlank).distinct().map(data -> {
                RoomProdRelationEntity temp = new RoomProdRelationEntity();
                temp.setRoomId(roomId);
                temp.setIlId(ilId);
                temp.setType(roomAssociationType);
                temp.setOfcode(data);
                temp.setRoomDraftRecordId(roomDraftRecordId);
                return temp;
            }).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(insertRoomProdRelation)) {
                saveBatch(insertRoomProdRelation);
            }
        }else{
            LambdaQueryWrapper<RoomProdRelationEntity> delQuery = Wrappers.lambdaQuery();
            delQuery.eq(RoomProdRelationEntity::getType, roomAssociationType)
            .and(w->w.eq(RoomProdRelationEntity::getIlId,ilId).or().eq(RoomProdRelationEntity::getRoomDraftRecordId,roomDraftRecordId))
            ;
            remove(delQuery);
        }
    }

    /**
     * 获取原有的关联数据
     * @param roomDraftRecordId
     * @param roomId
     * @param ilId
     * @param roomAssociationType
     * @return
     */
    private List<RoomProdRelationEntity> getOldRoomProdRelationEntities(Integer roomDraftRecordId, String roomId, Integer ilId, Integer roomAssociationType) {
        List<RoomProdRelationEntity> roomProdRelationEntities = new ArrayList<>();
        if(Objects.equals(roomDraftRecordId,0) && Objects.equals(0,ilId)){
            return roomProdRelationEntities;
        }
        RoomDraftRecordEntity roomDraftRecordEntity = roomDraftRecordMapper.selectById(roomDraftRecordId);

//            当未查询到草稿时使用房间的信息查询原有的
        if (Objects.isNull(roomDraftRecordEntity)) {
            LambdaQueryWrapper<RoomProdRelationEntity> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(RoomProdRelationEntity::getIlId, ilId)
                    .eq(RoomProdRelationEntity::getRoomId, roomId)
                    .eq(RoomProdRelationEntity::getType, roomAssociationType);
            roomProdRelationEntities = list(queryWrapper);
        }else{
            LambdaQueryWrapper<RoomProdRelationEntity> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(RoomProdRelationEntity::getRoomDraftRecordId, roomDraftRecordId)
                    .eq(RoomProdRelationEntity::getType, roomAssociationType);
            roomProdRelationEntities = list(queryWrapper);
        }
        return roomProdRelationEntities;
    }
}




